package com.sto.stoapp.util;

import android.content.Context;
import android.view.View;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.listener.OnBackClickListener;
import com.sto.stoapp.widget.BaseDialog;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wj on 2016-11-28.
 */

public class BillFilterUtil {

    public static boolean isFilter(String billNo, Context context){

        //获取过滤list字符串
        String billListStr= StoFilePreference.getInstance(context).getBillList();

        if("".equals(billListStr))
            return false;

        try {
            JSONArray array=new JSONArray(billListStr);
            List<String> billList=new ArrayList<String>();

            for (int i=0;i<array.length();i++){
                billList.add(array.getString(i));
            }

            if(billList.contains(billNo)){
                return true;
            }

        } catch (JSONException e) {
            e.printStackTrace();
            return false;
        }catch (Exception e){
            return false;
        }

        return false;
    }

    /**
     * 获取过滤数据
     * @param context
     * @return
     */
    public static List<String> getFilters(Context context){

        List<String> datas=new ArrayList<String>();

        //获取过滤list字符串
        String billListStr= StoFilePreference.getInstance(context).getBillList();

        if("".equals(billListStr))
            return datas;

        try {
            JSONArray array=new JSONArray(billListStr);

            for (int i=0;i<array.length();i++){
                datas.add(array.getString(i));
            }

        } catch (JSONException e) {
            e.printStackTrace();
            return datas;
        }catch (Exception e){
            return datas;
        }

        return datas;
    }

    /**
     * 过滤扫描
     * @param bill
     * @param context
     */
    public static void billFilter(String bill ,Context context){
        boolean isFilter= isFilter(bill,context);
        if(!isFilter){
            Helper.showFilterSoundToast(context,
                    context.getString(R.string.code_filter));
            final BaseDialog dialog = new BaseDialog(context);
            dialog.setTitles(bill+context.getString(R.string.filter_warn));
            dialog.show();
            dialog.getvMessage().setVisibility(View.GONE);
            dialog.getvPresentation().setVisibility(View.GONE);
            dialog.setOnBackClickListener(new OnBackClickListener() {

                @Override
                public void onBackClickListener(boolean res) {
                    // TODO Auto-generated method stub
                }
            });
        }

        return;
    }

}


